<?php
 jimport('joomla.application.component.view');
class svViewSunghiep extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_add();
				break;
			case 'edit':
				$this->_edit();
				break;
			case 'save':
				$this->_save();
				break;
			case 'cancel':
				$this->_cancel();
				break;
			case 'remove':
				$this->_remove();
				break;
			default :
				$this->_default();
				break;
		}
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		// them toolbar
		$this->_gridToolbar();
		// ket noi voi model
		$model=$this->getModel('sunghiep');
		// lay gia tri list cho cac dropbox
		$iddonviquanly=JRequest::getVar('iddonviquanly');
		$this->assignRef('iddonviquanly', $iddonviquanly);
		$idphongban=JRequest::getVar('idphongban');
		$this->assignRef('idphongban', $idphongban);
		$idcanbo=JRequest::getVar('idcanbo');
		$this->assignRef('idcanbo', $idcanbo);
		$idloaisunghiep=JRequest::getVar('idloaisunghiep');
		$this->assignRef('idloaisunghiep', $idloaisunghiep);
		$phongbanList=$model->getphongbanList($iddonviquanly);
		$this->assignRef('phongbanList', $phongbanList);
		$canboList=$model->getcanboList($idphongban);
		$this->assignRef('canboList', $canboList);
		$loaisunghiepList=$model->getAllLoaisunghiep();
		$this->assignRef('loaisunghiepList', $loaisunghiepList);
		$donviList=$model->getdonviList();
		$this->assignRef('donviList', $donviList);
		// cac bien gia tri
		$page=JRequest::getInt('page');
		$this->assignRef('page', $page);
		$kaka=0;
		if($iddonviquanly!=0)
		{
			$result=$model->getAllSunghiep1($iddonviquanly,$page);
			$count=$model->getAllSunghiepCount1($iddonviquanly);
			$kaka=1;
		}
		if($idphongban!=0)
		{
			$result=$model->getAllSunghiep2($idphongban,$page);
			$count=$model->getAllSunghiepCount2($idphongban);
		}
		if($idcanbo!=0)
		{
			$kaka=1;
			$result=$model->getAllSunghiep3($idcanbo,$page);
			$count=$model->getAllSunghiepCount3($idcanbo);
		}
		if($idloaisunghiep!=0)
		{
			$kaka=1;
			$result=$model->getAllSunghiep4($idloaisunghiep,$page);
			$count=$model->getAllSunghiepCount4($idloaisunghiep);
		}
		if($kaka==0)
		{
			$result=$model->getAllSunghiep($page);
			$count=$model->getAllSunghiepCount();
		}
		$title='Quản lý theo dõi sự nghiệp các cá nhân';
		// set layout
		$this->assignRef('dataObject', $result);
		$this->assignRef('count', $count);
		$this->assignRef('title', $title);
		$this->setLayout('default');
	}
	function _add()
	{
		$this->_formToolbar('Thêm mới một cột mốc sự nghiệp');
		// chon cac donviquanly
		$model=$this->getModel('sunghiep'); 
		$loaisunghiep=$model->getAllLoaisunghiep();
		$canbo=$model->getAllCanbo();
		// set layout
		$this->assignRef('loaisunghiep', $loaisunghiep);
		$this->assignRef('canbo', $canbo);
		$this->setLayout('add');
	}
	function _edit()
	{
		// toolbar
		$this->_formToolbar('Chỉnh sửa một item');
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idsunghiep  = $idlist[0];
		if($idsunghiep==0) $idsunghiep=JRequest::getInt('idsunghiep');
		// lay du lieu tu model
		$model=$this->getModel('sunghiep');
		$sunghiep=$model->getSunghiepByID($idsunghiep);
		$loaisunghiep=$model->getAllLoaisunghiep();
		$canbo=$model->getAllCanbo();
		// set layout
		$this->assignRef('sunghiep', $sunghiep);
		$this->assignRef('loaisunghiep', $loaisunghiep);
		$this->assignRef('canbo', $canbo);
		$this->setLayout('edit');
	}
	function _save()
	{
		// lay du lieu
		$idcanbo=JRequest::getInt('idcanbo');
		$mieuta = JRequest::getVar('mieuta', 'Mieu ta ve phong ban', 'POST','validation type',JREQUEST_ALLOWHTML);
		$idloaisunghiep=JRequest::getInt('idloaisunghiep');
		$tieude=JRequest::getVar('tieude');
		$diadiem=JRequest::getVar('diadiem');
		$batdau=JRequest::getVar('batdau');
		$ketthuc=JRequest::getVar('ketthuc');
		$thanhtich=JRequest::getVar('thanhtich');
		$tinhtrang=JRequest::getVar('tinhtrang');
		// xu ly yeu cau edit/insert
		$model=$this->getModel('sunghiep');
		$idsunghiep=JRequest::getInt('idsunghiep');
		if($idsunghiep==0)// xac thuc viec them mot item
		{
			$exec=$model->addNewSunghiep($idcanbo,$idloaisunghiep,$tieude,$diadiem,$batdau,$ketthuc,$thanhtich,$mieuta,$tinhtrang);
		}
		else//xac thuc viec edit mot item
		{
			$exec=$model->editSunghiep($idcanbo,$idloaisunghiep,$tieude,$diadiem,$batdau,$ketthuc,$thanhtich,$mieuta,$tinhtrang,$idsunghiep);
		}
		echo $exec;
		$this->_default();
	}
	function _cancel()
	{
		header('Location:index.php?option=com_svquanlycanbo&view=sunghiep');
	}
	function _remove()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idphongban    = $idlist[0];
		// thuc hien yeu cau remove
		$model=$this->getModel('phongban');
		$exec=$model->delPhongban($idphongban);
		echo $exec;
		$this->_default();
		
	}
	// them cac button tren thanh toolbar
	function _gridToolbar()
	{
		JToolBarHelper::title('Quản lý cột mốc trong sự nghiệp các cán bộ.!');
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function _formToolbar($title)
	{
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	
}
?>